<template>
	<view class="wrap">
	
		<u-form :model="model" :rules="rules" ref="uForm" :errorType="errorType" >
		
			<u-form-item label-position="left" label="旧密码" label-width="150" prop="oldPassword">
				<u-input :password-icon="true" :border="true" type="password" v-model="model.oldPassword" placeholder="请输入旧密码"></u-input>
			</u-form-item>
			<u-form-item label-position="left" label="新密码" label-width="150" prop="password">
				<u-input :password-icon="true" :border="true" type="password" v-model="model.password" placeholder="请输入新密码"></u-input>
			</u-form-item>
			<u-form-item label-position="left" label="确认新密码" label-width="150" prop="rePassword">
				<u-input :border="true" type="password" v-model="model.rePassword" placeholder="请确认新密码"></u-input>
			</u-form-item>
		</u-form>
		<u-button @click="submit" size="default" type="primary" :ripple="true">提交</u-button>
	</view>
</template>

<script>
	import md5Libs from "uview-ui/libs/function/md5";
	const db = uniCloud.database();
	export default {
		data() {
			let that = this;
			return {
				model: {
					oldPassword: '',
					password: '',
					rePassword:'',
					loadding:false
				},
				rules: {
					password: [
						{
							required: true,
							message: '请输入密码',
							trigger: ['change','blur'],
						},
						{
							// 正则不能含有两边的引号
							pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]+\S{5,12}$/,
							message: '需同时含有字母和数字，长度在6-12之间',
							trigger: ['change','blur'],
						}
					],
					rePassword: [
						{
							required: true,
							message: '请重新输入密码',
							trigger: ['change','blur'],
						},
						{
							validator: (rule, value, callback) => {
								return value === this.model.password;
							},
							message: '两次输入的密码不相等',
							trigger: ['change','blur'],
						}
					],
				},
				border: false,
				errorType: ['message'],
			};
		},
		onLoad() {
		},
		computed: {
			borderCurrent() {
				return this.border ? 0 : 1;
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			// 提交前校验表单信息
			submit() {
				let that = this
				this.$refs.uForm.validate(valid => {
					if (valid) {
						that.restPsw()();
					}
				});
			},
			
			// 修改密码
			restPsw(){
				let that = this
				uni.showLoading({
					title:''
				})
				
				this.request('common/user/updatePwd', {
					oldPassword: that.model.oldPassword,
					newPassword: that.model.password
				}).then(res => {
					if(res.code==0){
						uni.showModal({
							showCancel: false,
							content: res.msg,
							success:function(res){
								if (res.confirm) {
									uni.reLaunch({
										url:'/pages/login/login',
										fail(e) {
											console.log(e);
										}
									})
								}
							}
						})
						
					}else{
						uni.showModal({
							showCancel: false,
							content: res.msg
						})
					}
				}).catch().finally(() => {
					uni.hideLoading()
				})
			},


			errorChange(index) {
				if(index == 0) this.errorType = ['message'];
				if(index == 1) this.errorType = ['toast'];
				if(index == 2) this.errorType = ['border-bottom'];
				if(index == 3) this.errorType = ['border'];
			}
		}
	};
</script>

<style scoped lang="scss">
	.wrap {
		padding: 30rpx;
	}
	
	.alternative {
		color: $u-tips-color;
		display: flex;
		justify-content: space-between;
		margin-top: 30rpx;
		margin-bottom: 30rpx;
	}
	
	.agreement {
		display: flex;
		align-items: center;
		margin: 40rpx 0;
		.agreement-text {
			padding-left: 8rpx;
			color: $u-tips-color;
		}
	}
</style>
